﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaiTu.Framework;
using System.IO;
using System.Security.AccessControl;

namespace BaiTu.Common.Helpers
{
    /// <summary>
    /// 目录帮助类  帮助类不允许实例化
    /// </summary>
    public sealed class DirectoryHelper : Framework.Interfaces.ISingletonDependency
    {
        /// <summary>
        /// 判断目录是否存在
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static InnerResult DirectoryExists(string path)
        {
            InnerResult innerResult = new InnerResult();
            try
            {
                innerResult.Success = Directory.Exists(path);
            }
            catch (Exception ex)
            {
                innerResult = ex.GetInnerResult();
            }
            return innerResult;
        }
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static InnerResult CreateDirectory(string path)
        {
            InnerResult innerResult = new InnerResult();
            try
            {
                Directory.CreateDirectory(path);
                innerResult.Success = true;
            }
            catch (Exception ex)
            {
                innerResult = ex.GetInnerResult(string.Format("创建目录失败，目录路径为:{0}", path));
            }
            return innerResult;
        }

        /// <summary>
        /// 删除指定目录
        /// </summary>
        /// <param name="path">目录路径</param>
        /// <param name="recursive">是否递归删除  如果为true 则删除所有子目录</param>
        /// <returns></returns>
        public static InnerResult DeleteDirectory(string path, bool recursive = false)
        {
            InnerResult innerResult = new InnerResult();
            ///删除目录前首先检测目录是否存在

            if ((innerResult = DirectoryExists(path)).Success == false)
            {
                innerResult.ErrorMessage = string.Format("要删除的目录不存在，目录路径为:{0}", path);
            }
            innerResult.Clear();
            try
            {
                Directory.Delete(path, recursive);
                innerResult.Success = true;
            }
            catch (Exception ex)
            {
                innerResult = ex.GetInnerResult(string.Format("删除目录失败，目录路径为:{0}", path));
            }
            return innerResult;
        }

    }
}
